package com.liangzhong.model.ladderMatch
{
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.ladderMatch.vo.ApplyVo;
	import com.liangzhong.model.ladderMatch.vo.LadderInfoVo;
	import com.liangzhong.model.ladderMatch.vo.MatchTimeVo;
	import com.liangzhong.model.ladderMatch.vo.RankVo;
	import com.liangzhong.model.match.vo.MatchResultVo;
	
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;

	/**
	 *天梯信息 
	 * @author Administrator
	 * 
	 */	
	public class LadderMatchDataManger
	{
		private var _ladderInfovo:LadderInfoVo;
		/**
		 *报名列表 
		 */
		private var _applyList:Array;
		/**
		 *排名前十 
		 */
		private var _topList:Array;
		/**
		 *比赛时间 
		 */
		public var matchTimeList:Array;
		/**
		 *更多的天梯排名  
		 */
		private var _moreRankDic:Dictionary;
		/**
		 *更新更多天梯排名时间 
		 */
		public var updataMoreRankTime:int;
		public function LadderMatchDataManger()
		{
			analysisLadderTime();
		}
		/**
		 *获得天梯信息 
		 * @param $obj
		 * 
		 */
		public function getLadderInfo($obj:Object):void
		{
			if(_ladderInfovo == null)_ladderInfovo = new LadderInfoVo();
			_ladderInfovo.analysis($obj.integral);
			
			
//			_ladderInfovo.topThird($obj.preTop3  );
			_ladderInfovo.analysisMatchResult($obj.TTmatchResult);
		}

		public function get ladderInfovo():LadderInfoVo
		{
			return _ladderInfovo;
		}

		/**
		 *获得报名列表 
		 * 
		 */
		public function getapplyList($arr:Array):Array
		{
			if($arr == null)return [];
			if(_applyList == null)_applyList = [];
			_applyList.splice(0,_applyList.length);
			var len:int = $arr.length
			var vo:ApplyVo;
			var arr:Array = [];
			for(var i:int = 0;i < len;i++)
			{
				vo = new ApplyVo();
				vo.analysis($arr[i]);
				_applyList.push(vo);
				arr.push(vo);
			}
			return arr;
			
		}
		/**
		 *获取报名列表 
		 * @return 
		 * 
		 */		
		public function get applyList():Array
		{
			return _applyList;
		}
		/**
		 *清空报名列表 
		 * 
		 */		
		public function clearApplyist():void
		{
			if(_applyList == null)return;
			_applyList.splice(0,_applyList.length);
		}
		/**
		 * 前十排名  
		 * @param $obj
		 * 
		 */
		public function gettopList($obj:Object):void
		{
			if(_topList == null)_topList = [];
			_topList.splice(0,_topList.length);
			var arr:Array = $obj.top10list;
			var len:int = arr.length;
			var vo:RankVo;
			for(var i:int = 0;i < len;i++)
			{
				vo = new RankVo();
				vo.analysis(arr[i]);
				_topList.push(vo);
			}
		}
		public function get topList():Array
		{
			return _topList;
		}
		
		
		/**
		 *解析天梯比赛时间 
		 * 
		 */
		public function analysisLadderTime():void
		{
			matchTimeList = [];
			var xml:XML = ResourceManager.instance.getResource(ResourceConst.LADDERMATCHTIME);
//			trace(xml.seasonTime);
			var timeXml:XML = XML(ResourceManager.instance.getResource(ResourceConst.LADDERMATCHTIME)).time[0];
			var releaseVersion :String;
			var vo:MatchTimeVo;
			for each(var item :XML in timeXml.item)
			{
				vo = new MatchTimeVo();
				vo.starTime = item.@starTime;
				vo.endTime = item.@endTime;
				matchTimeList.push(vo);
			}
		}
		
		
		/**
		 *更多的天梯排名  
		 * @param $obj
		 * 
		 */
		public function analysyMoreRank($obj:Object):void
		{
			if(_moreRankDic == null)_moreRankDic = new Dictionary();
//			_moreRankDic["a"] = [];
//			_moreRankDic["b"] = [];
//			_moreRankDic["c"] = [];
//			_moreRankDic["d"] = [];
//			_moreRankDic["s"] = [];
			var arr:Array;
			var len:int;
			var i:int ;
			var vo:RankVo;
			var tempList:Array;
			for(var str:String in $obj)
			{
				_moreRankDic[str] = [];
				arr = $obj[str];
				len = arr.length;
				for(i = 0;i < len;i++)
				{
					vo = new RankVo();
					vo.analysis(arr[i]);
					_moreRankDic[str].push(vo);
				}
				tempList = _moreRankDic[str];
				tempList.sort(sortRank);
				function sortRank(a:RankVo,b:RankVo):Number
				{
					if(a.sort > b.sort)return 1;
					if(a.sort < b.sort)return -1;
					return 0;
				}
			}
			
		}
		/**
		 *根据类型获取排名
		 * ABCDS 
		 * @param $type
		 * @return 
		 * 
		 */
		public function getLadderRankByType($type:String):Array
		{
			if(_moreRankDic == null)return null;
			return _moreRankDic[$type];
		}
		/**
		 *天梯等级转换 
		 * @return 
		 * 
		 */
		public  function levelChange():String
		{
			switch(ladderInfovo.ladderlevel)
			{
				case 1:
					return "D";
				case 2:
					return "C";
				case 3:
					return "B";
				case 4:
					return "A";
				case 5:
					return "S";
			}
			return "D";
		}
		
		/**
		 *获得一场 天梯比赛结果  
		 * MatchResultVo 
		 */
		public function getMatchOnceResult(resultVo:MatchResultVo):void
		{
			if(_ladderInfovo == null)return;
			_ladderInfovo.getMatchOnceresult(resultVo);
		}
		/**
		 *门票收入 
		 * 
		 */
		public function ticketIncome():int
		{
			var lv:int = AppData.getInstance().clubDatamanager.clubInfo.level;
			var inCome:int;
			if(lv < 6)
			{
				inCome = 50000;
			}else if(lv < 10)
			{
				inCome = 150000;
			}
			else if(lv < 15)
			{
				inCome = 250000;
			}
			else if(lv < 19)
			{
				inCome = 350000;
			}
			else if(lv < 30)
			{
				inCome = 450000;
			}
			else if(lv < 38)
			{
				inCome = 550000;
			}
			else if(lv < 41)
			{
				inCome = 650000;
			}
			else if(lv < 45)
			{
				inCome = 750000;
			}else
			{
				inCome = 850000
			}
			return inCome;
		}
	}
}